package com.agency.dao;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.agency.entity.Authentication;
import com.agency.util.HibernateUtil;

public class AuthenticationDao {

    public void addAuthentication(Authentication authentication) {
        Transaction trns = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            trns = session.beginTransaction();
            session.save(authentication);
            session.getTransaction().commit();
        } catch (RuntimeException e) {
            if (trns != null) {
                trns.rollback();
            }
            e.printStackTrace();
        } finally {
            session.flush();
            session.close();
        }
    }

    public void deleteAuthentication(int idAuthentication) {
        Transaction trns = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            trns = session.beginTransaction();
            Authentication authentification = (Authentication) session.load(Authentication.class, new Integer(idAuthentication));
            session.delete(authentification);
            session.getTransaction().commit();
        } catch (RuntimeException e) {
            if (trns != null) {
                trns.rollback();
            }
            e.printStackTrace();
        } finally {
            session.flush();
            session.close();
        }
    }

    public void updateAuthentication(Authentication authentification) {
        Transaction trns = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            trns = session.beginTransaction();
            session.update(authentification);
            session.getTransaction().commit();
        } catch (RuntimeException e) {
            if (trns != null) {
                trns.rollback();
            }
            e.printStackTrace();
        } finally {
            session.flush();
            session.close();
        }
    }

    public List<Authentication> getAllAuthentications() {
        List<Authentication> Authentications = new ArrayList<Authentication>();
        Transaction trns = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            trns = session.beginTransaction();
            Authentications = session.createQuery("select concat(username, ' ', password) as authentication from Authentication").list();
        } catch (RuntimeException e) {
            e.printStackTrace();
        } finally {
            session.flush();
            session.close();
        }
        return Authentications;
    }

    public List<Authentication> getAuthenticationById(String idAuthentication) {
        System.out.println(idAuthentication);
//        Authentication cust = null;
        Transaction trns = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            trns = session.beginTransaction();
            String queryString = "from Authentication where idAuthentication = :id";
            Query query = session.createQuery(queryString);
            query.setString("id", idAuthentication);
            //cust = (Authentication) query.uniqueResult();
            List<Authentication> list = query.list();
            if (list.size() > 0) {
                return list;
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
        } finally {
            session.flush();
            session.close();
        }
        return null;
    }
	
}
